//
//  JXRAFNetWorkTool.m
//  JXRMinor
//
//  Created by dllo on 15/10/12.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "JXRAFNetWorkTool.h"
#import <AFNetworking.h>

@implementation JXRAFNetWorkTool

+ (void)getNetWithURL:(NSString *)url
                 body:(id)body
           resultKind:(JXRResult)result
           headerFile:(NSDictionary *)headerFile
              success:(BlockOfSuccess)success
              failure:(BlockOfFailure)failure{
    // 去掉网址中的"/" 作为缓存数据的标识
    NSString *str = [url stringByReplacingOccurrencesOfString:@"/" withString:@""];
    //1. 获取网络请求管理类
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    //3. 为网络请求加请求头
    if (headerFile) {
        for (NSString *key in headerFile) {
            [manager.requestSerializer setValue:headerFile[key] forHTTPHeaderField:key];
        }
    }
    
    //4. 设置相应的解析对象即返回的数据类型
    switch (result) {
        case JXRData:
            // HTTP 返回Data类型
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case JXRJSON:
            // JSON返回 , 默认使用JSON 所以可以直接break
            //            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case JXRXML:
            // XML 返回XML数据类型.
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    //2. 设置网络请求返回值所支持的参数类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil]];
    
    //5. 发送网络请求
    [manager GET:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            
            NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            
            NSString *sandBoxPath = sandBox[0];
            NSString *caches = [sandBoxPath stringByAppendingPathComponent:str];
            
            [NSKeyedArchiver archiveRootObject:responseObject toFile:caches];
            
            //成功回调
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
            // 先反归档! 如果有就直接用, 没有就在下面归档
        //失败回调
        failure(error);
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        
        NSString *sandBoxPath = sandBox[0];
        NSString *caches = [sandBoxPath stringByAppendingPathComponent:str];
        
        id responseObject = [NSKeyedUnarchiver unarchiveObjectWithFile:caches];
        
        if (responseObject != nil) {
            
            success(responseObject);
        }
        
    }];
    
}

+ (void)PostNetWithURL:(NSString *)url
                  body:(id)body
            resultKind:(JXRResult)result
          requsetStyle:(JXRRequestStyle)requestStyle
            headerFile:(NSDictionary *)headerFile
               success:(BlockOfSuccess)success
               failure:(BlockOfFailure)failure{
    // 1. 获取网络请求管理类
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    
    // 3.设置网络请求体body的类型
    switch (requestStyle) {
        case JXRRequestJSON:
            
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            
            break;
            
        case JXRRequestString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, id parameters, NSError *__autoreleasing *error) {
                return parameters;
            }];
            
            break;
        default:
            break;
    }
    
    
    //4. 为网络请求加请求头
    if (headerFile) {
        for (NSString *key in headerFile) {
            [manager.requestSerializer setValue:headerFile[key] forHTTPHeaderField:key];
        }
    }
    
    // 5. 网络请求返回值的数据类型
    switch (result) {
        case JXRData:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case JXRJSON:
            break;
        case JXRXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    //2. 设置网络请求返回值支持的类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil]];
    
    //6. 发送网络请求
    [manager POST:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            success(responseObject);
//             [[JXRDataCachesTool shareJXRDataCachesTool] saveCachesWithPlistNameUrl:url  responseObject:responseObject];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

                // 先反归档! 如果有就直接用, 没有就在下面归档
//                id cachesResult =  [[JXRDataCachesTool shareJXRDataCachesTool] cachesWithurl:url];
        
//                if (cachesResult) {
//                    
//                    success(cachesResult);
//                    
//                }else{
                    if (error) {
                    // 失败回调
                    failure(error);
                    }
//                }
    }];
    
}


@end
